package bdz.projects.cbt.ui
{
	import bdz.projects.cbt.core.CBTProduct;
	import bdz.projects.cbt.core.CBTProducts;
	import bdz.projects.cbt.events.CBTGameEvent;
	import bdz.projects.cbt.events.CBTUIEvent;
	
	import flash.events.MouseEvent;
	
	public class CBTProductList extends CBTList {
		
		
		private var _listref:CBTProducts;
		public function CBTProductList() {
			
			_renderer = "ProductItemRenderer";
			super();
			_channel.addEventListener(CBTGameEvent.GAME_CHANGE, onGameChanged);
			_channel.addEventListener(CBTGameEvent.GAME_STARTED, onGameChanged);
		}
		override protected function onGameChanged(evt:CBTGameEvent):void{
			
			_listref = evt.data.products;
			dataProvider = evt.data.products.productList;
			
			
		}
		
		override protected function itemsRendered():void{
			_channel.dispatchEvent(new CBTGameEvent(CBTGameEvent.PRODUCTS_REFRESHED, dataProvider));
		}
		override protected function onDoubleClickItem(evt:MouseEvent):void{
			super.onDoubleClickItem(evt);
			_channel.dispatchEvent(new CBTUIEvent(CBTUIEvent.PRODUCT_ITEM_SELECTED, {type:"products", lastSelected:_lastSelected, currentSelected:evt.target, info:_lastSelected.info}));
			_channel.dispatchEvent(new CBTUIEvent(CBTUIEvent.SHOW_BUY_INTERFACE));
		}
		override protected function onSelectItem(evt:MouseEvent):void{
			super.onSelectItem(evt);
			_channel.dispatchEvent(new CBTUIEvent(CBTUIEvent.PRODUCT_ITEM_SELECTED, {type:"products", lastSelected:_lastSelected, currentSelected:evt.target, info:_lastSelected.info}));	
		}
		public function findProduct(n:String):CBTProduct{
			return _listref.findProduct(n);
		}
		
	}
}